package com.ee.chat.cps.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ee.chat.common.bean.Result;
import com.ee.chat.common.dao.UserCreditLogMapper;
import com.ee.chat.common.entity.Channel;
import com.ee.chat.cps.admin.service.IChannelOperationService;
import com.ee.chat.cps.admin.service.IReportService;
import com.ndhutu.ee.util.DateUtil;
import com.ndhutu.ee.util.PageUtil;
import com.ndhutu.ee.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements IReportService {

    @Resource
    UserCreditLogMapper userCreditLogMapper;
    @Resource
    IChannelOperationService operationService;

    /**
     * 获取我的报表列表
     * @Author AppiJ
     * @Date 2020/3/30 15:11
     * @param date  搜索日期
     * @return
     */
    @Override
    public Result list(HttpServletRequest request, Integer page, Integer limit, String date) throws Exception {

        Map<String, Object> data = new HashMap<>();

        HttpSession session = request.getSession(true);
        Channel channel = (Channel) session.getAttribute("channelUser");

        String beginDate = null, endDate = null;
        if(!StringUtil.isEmpty(date)){
            String[] arr = date.split(" - ");
            beginDate = arr[0];
            endDate = arr[1];
        }

        Page<Map<String, Object>> p = new Page<>(page, limit);
        IPage<Map<String, Object>> iPage = userCreditLogMapper.queryReportList(p, channel.getUserId(), channel.getId(), beginDate, endDate);
        List<Map<String, Object>> datas = iPage.getRecords();

        data.put("datas", datas);
        data.put("total", iPage.getTotal());

        operationService.addOperationLog(request, "查看我的报表列表数据");

        return Result.returnSuccess(data);
    }

    /**
     * 获取报表明细列表
     * @Author AppiJ
     * @Date 2020/4/1 14:59
     * @param time  日期时间
     * @param type  类型
     * @return
     */
    @Override
    public Result detailList(HttpServletRequest request, String time, Integer type, Integer page, Integer limit) throws Exception {

        Map<String, Object> data = new HashMap<>();

        HttpSession session = request.getSession(true);
        Channel channel = (Channel) session.getAttribute("channelUser");

        Date beginTime = null, endTime = null;
        if(!StringUtil.isEmpty(time)){
            String[] arr = time.split(" - ");
            beginTime = DateUtil.parse(arr[0]);
            endTime = DateUtil.parse(arr[1]);
        }

        List<Map<String, Object>> reportDetails =
                userCreditLogMapper.list(channel.getUserId(), null, type, beginTime, endTime, PageUtil.getOffset(page, limit), limit);

        data.put("reportDetails", reportDetails);
        data.put("total", userCreditLogMapper.listCount(channel.getUserId(),null,null, beginTime, endTime));

        operationService.addOperationLog(request, "查看报表明细列表数据");

        return Result.returnSuccess(data);
    }
}
